home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_groff.idb / usr / freeware / bin / grog.z / grog
Text File  |  2002-04-08  |  4KB  |  211 lines

  1. #!/usr/bin/perl5
  2. # grog -- guess options for groff command
  3. # Inspired by doctype script in Kernighan & Pike, Unix Programming
  4. # Environment, pp 306-8.
  5.  
  6. $prog = $0;
  7. $prog =~ s@.*/@@;
  8.  
  9. $sp = "[\\s\\n]";
  10.  
  11. push(@command, "groff");
  12.  
  13. while ($ARGV[0] =~ /^-./) {
  14.     $arg = shift(@ARGV);
  15.     $sp = "" if $arg eq "-C";
  16.     &usage(0) if $arg eq "-v" || $arg eq "--version";
  17.     &help() if $arg eq "--help";
  18.     last if $arg eq "--";
  19.     push(@command, $arg);
  20. }
  21.  
  22. if (@ARGV) {
  23.     foreach $arg (@ARGV) {
  24.     &process($arg, 0);
  25.     }
  26. }
  27. else {
  28.     &process("-", 0);
  29. }
  30.  
  31. sub process {
  32.     local($filename, $level) = @_;
  33.     local(*FILE);
  34.  
  35.     if (!open(FILE, $filename eq "-" ? $filename : "< $filename")) {
  36.     print STDERR "$prog: can't open \`$filename': $!\n";
  37.     exit 1 unless $level;
  38.     return;
  39.     }
  40.     while (<FILE>) {
  41.     if (/^\.TS$sp/) {
  42.         $_ = <FILE>;
  43.         if (!/^\./) {
  44.         $tbl++;
  45.         $soelim++ if $level;
  46.         }
  47.     }
  48.     elsif (/^\.EQ$sp/) {
  49.         $_ = <FILE>;
  50.         if (!/^\./ || /^\.[0-9]/) {
  51.         $eqn++;
  52.         $soelim++ if $level;
  53.         }
  54.     }
  55.     elsif (/^\.GS$sp/) {
  56.         $_ = <FILE>;
  57.         if (!/^\./) {
  58.         $grn++;
  59.         $soelim++ if $level;
  60.         }
  61.     }
  62.     elsif (/^\.G1$sp/) {
  63.         $_ = <FILE>;
  64.         if (!/^\./) {
  65.         $grap++;
  66.         $pic++;
  67.         $soelim++ if $level;
  68.         }
  69.     }
  70.     elsif (/^\.PS$sp([ 0-9.<].*)?$/) {
  71.         if (/^\.PS\s*<\s*(\S+)/) {
  72.         $pic++;
  73.         $soelim++ if $level;
  74.         &process($1, $level);
  75.         }
  76.         else {
  77.         $_ = <FILE>;
  78.         if (!/^\./ || /^\.ps/) {
  79.             $pic++;
  80.             $soelim++ if $level;
  81.         }
  82.         }
  83.     }
  84.     elsif (/^\.R1$sp/ || /^\.\[$sp/) {
  85.         $refer++;
  86.         $soelim++ if $level;
  87.     }
  88.     elsif (/^\.[PLI]P$sp/) {
  89.         $PP++;
  90.     }
  91.     elsif (/^\.P$/) {
  92.         $P++;
  93.     }
  94.     elsif (/^\.(PH|SA)$sp/) {
  95.         $mm++;
  96.     }
  97.     elsif (/^\.TH$sp/) {
  98.         $TH++;
  99.     }
  100.     elsif (/^\.SH$sp/) {
  101.         $SH++;
  102.     }
  103.     elsif (/^\.([pnil]p|sh)$sp/) {
  104.         $me++;
  105.     }
  106.     elsif (/^\.Dd$sp/) {
  107.         $mdoc++;
  108.     }
  109.     elsif (/^\.(Tp|Dp|De|Cx|Cl)$sp/) {
  110.         $mdoc_old = 1;
  111.     }
  112.     # In the old version of -mdoc `Oo' is a toggle, in the new it's
  113.     # closed by `Oc'.
  114.     elsif (/^\.Oo$sp/) {
  115.         $Oo++;
  116.         s/^\.Oo/\. /;
  117.         redo;
  118.     }
  119.     # The test for `Oo' and `Oc' not starting a line (as allowed by the
  120.     # new implementation of -mdoc) is not complete; it assumes that
  121.     # macro arguments are well behaved, i.e., "" is used within "..." to
  122.     # indicate a doublequote as a string element, and weird features
  123.     # like `.foo a"b' are not used.
  124.     elsif (/^\..* Oo( |$)/) {
  125.         s/\\\".*//;
  126.         s/\"[^\"]*\"//g;
  127.         s/\".*//;
  128.         if (s/ Oo( |$)/ /) {
  129.         $Oo++;
  130.         }
  131.         redo;
  132.     }
  133.     elsif (/^\.Oc$sp/) {
  134.         $Oo--;
  135.         s/^\.Oc/\. /;
  136.         redo;
  137.     }
  138.     elsif (/^\..* Oc( |$)/) {
  139.         s/\\\".*//;
  140.         s/\"[^\"]*\"//g;
  141.         s/\".*//;
  142.         if (s/ Oc( |$)/ /) {
  143.         $Oo--;
  144.         }
  145.         redo;
  146.     }
  147.     if (/^\.so$sp/) {
  148.         chop;
  149.         s/^.so *//;
  150.         s/\\\".*//;
  151.         s/ .*$//;
  152.         &process($_, $level + 1) unless /\\/ || $_ eq "";
  153.     }
  154.     }
  155.     close(FILE);
  156. }
  157.  
  158. sub usage {
  159.     local($exit_status) = $_;
  160.     print "GNU grog (groff) version 1.17.2\n";
  161.     exit $exit_status;
  162. }
  163.  
  164. sub help {
  165.     print "usage: grog [ option ...] [files...]\n";
  166.     exit 0;
  167. }
  168.  
  169. if ($pic || $tbl || $eqn || $grn || $grap || $refer) {
  170.     $s = "-";
  171.     $s .= "s" if $soelim;
  172.     $s .= "R" if $refer;
  173.     # grap must be run before pic
  174.     $s .= "G" if $grap;
  175.     $s .= "p" if $pic;
  176.     $s .= "g" if $grn;
  177.     $s .= "t" if $tbl;
  178.     $s .= "e" if $eqn;
  179.     push(@command, $s);
  180. }
  181.  
  182. if ($me > 0) {
  183.     push(@command, "-me");
  184. }
  185. elsif ($SH > 0 && $TH > 0) {
  186.     push(@command, "-man");
  187. }
  188. elsif ($PP > 0) {
  189.     push(@command, "-ms");
  190. }
  191. elsif ($P > 0 || $mm > 0) {
  192.     push(@command, "-mm");
  193. }
  194. elsif ($mdoc > 0) {
  195.     push(@command, ($mdoc_old || $Oo > 0) ? "-mdoc-old" : "-mdoc");
  196. }
  197.  
  198. push(@command, "--") if @ARGV && $ARGV[0] =~ /^-./;
  199.  
  200. push(@command, @ARGV);
  201.  
  202. # We could implement an option to execute the command here.
  203.  
  204. foreach (@command) {
  205.     next unless /[\$\\\"\';&()|<> \t\n]/;
  206.     s/\'/\'\\\'\'/;
  207.     $_ = "'" . $_ . "'";
  208. }
  209.  
  210. print join(' ', @command), "\n";
  211.